<template>
  <div class="sales-view">
    <el-card shadow="hover" :body-style="{ padding: '0 0 20px 0' }">
      <template v-slot:header>
        <div class="menu-wrapper">
          <el-menu
            :default-active="activeIndex"
            mode="horizontal"
            @select="onMenuSelect"
            class="sales-view-menu"
          >
            <el-menu-item index="1">销售额</el-menu-item>
            <el-menu-item index="2">访问量</el-menu-item>
          </el-menu>
          <div class="menu-right">
            <el-radio-group v-model="radioSelect" size="small">
              <el-radio-button label="今日"></el-radio-button>
              <el-radio-button label="本周"></el-radio-button>
              <el-radio-button label="本月"></el-radio-button>
              <el-radio-button label="今年"></el-radio-button>
            </el-radio-group>
            <el-date-picker
              type="daterange"
              v-model="date"
              range-separator="至"
              start-placeholde="开始日期"
              end-placeholde="结束日期"
              size="small"
              unlink-panels
              :picker-options="pickerOptions"
              class="sales-view-date-picker"
            />
          </div>
        </div>
      </template>
      <template>
        <div class="sales-view-chart-wrapper">
          <v-chart :options="chartOption" />
          <div class="sales-view-list">
            <div class="sales-view-title">排行榜</div>
            <div class="list-item-wrapper">
              <div class="list-item" v-for="item in rankData" :key="item.no">
                <!-- 加个+号，转换为整数型 -->
                <!-- <div class="list-item-no" :class="+item.no <= 3 ? 'top-no':'' "> {{ item.no }}</div> -->
                <!-- 更加优雅的写法，把多个class合并为一个class数组 -->
                <div :class="['list-item-no', +item.no <= 3 ? 'top-no' : '']">
                  {{ item.no }}
                </div>
                <div class="list-item-name">{{ item.name }}</div>
                <div class="list-item-money">{{ item.money }}</div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </el-card>
  </div>
</template>

<script>
import commonDataMixin from '@/mixins/commonDataMixin' //引入inject数据
export default {
    mixins: [commonDataMixin],
    data() {
        return {
           activeIndex: '1',
           radioSelect: '今日',
           date: null,
           pickerOptions: { 
               shortcuts: [{ 
                   text: '最近一周',
                   onClick(picker) { 
                       const start = new Date() //获取当前日期，后面改为七天前日期
                       const end = new Date() //获取当前日期
                       start.setTime(start.getTime() - 3600 * 24 * 1000 * 7) //设置七天前日期
                       picker.$emit('pick',[start,end])
                   }    
               },{ 
                   text: '最近一个月',
                   onClick(picker) { 
                       const start = new Date() 
                       const end = new Date() //获取当前日期
                       start.setTime(start.getTime() - 3600 * 24 * 1000 * 30) 
                       picker.$emit('pick',[start,end])
                   }
               },{ 
                   text: '最近三个月',
                   onClick(picker) { 
                       const start = new Date() 
                       const end = new Date() //获取当前日期
                       start.setTime(start.getTime() - 3600 * 24 * 1000 * 90) 
                       picker.$emit('pick',[start,end])
                   }
               }]
           },
           chartOption: { 

           }
        };
    },
    computed: { 
        rankData: function() { //判断渲染的是哪个排行榜
            return this.activeIndex === '1' ? this.orderRank : this.userRank
        }
    },
    watch: { //监控orderFullYear，如果获取到值就直接渲染年度销售额排行榜
        orderFullYear() { 
            this.render(this.orderFullYear,this.orderFullYearAxis,'年度销售额')
        }
    },
    methods: { 
        onMenuSelect(index) { 
            this.activeIndex = index
            if(index === '1') { //判断渲染哪个图表
                this.render(this.orderFullYear,this.orderFullYearAxis,'年度销售额')
            } else { 
                this.render(this.userFullYear,this.userFullYearAxis,'年度用户访问量')
            }
        },
        render(data, axis, title) { //决定渲染的是销售额图表还是访问量图表
            this.chartOption =  {
               title: { 
                   text: title,
                   textStyle: { 
                       fontSize: 12,
                       color: '#666'
                   },
                   left: 25,  //title有单独的grid
                   top: 20
               },
               xAxis: { 
                   type: 'category',
                   data: axis,
                   axisTick: { 
                       alignWithLabel: true,//设置短竖线在柱状图中间
                       lineStyle: { 
                           color: '#999'
                       }
                   },
                   axisLine: { 
                       lineStyle: { 
                           color: '#999' //x轴设置为灰色
                       }
                   },
                   axisLabel: { 
                       color: '#333' //x轴标签颜色为黑色
                   }
               },
               yAxis: {
                   axisLine: { 
                       show: false //隐藏y轴
                   },
                   axisTick: { 
                       show: false //隐藏y轴小横线
                   },
                   splitLine: { 
                       lineStyle: { 
                           type: 'dotted', //设置为点状虚线
                           color: '#eee'
                       }
                   }
                },
               series: [{
                   type: 'bar', 
                   barWidth: '35%',
                   data: data
               }],
               color: ['#3398DB'], //图表颜色
               grid: { 
                   top: 70,
                   left: 60,
                   right: 60,
                   bottom: 50
               }
            }
        }
    }
};
</script>

<style lang="scss" scoped>
.sales-view {
  margin-top: 20px;
  .menu-wrapper {
    position: relative;
    display: flex;
    .sales-view-menu {
      width: 100%;
      padding-left: 20px;
      .el-menu-item {
        height: 50px;
        line-height: 50px;
        margin: 0 20px;
      }
    }
    .menu-right {
      position: absolute;
      top: 0;
      right: 0;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      .sales-view-date-picker {
        margin-left: 20px;
      }
    }
  }
  .sales-view-chart-wrapper {
    display: flex;
    height: 270px;

    .echarts {
      flex: 0 0 70%;
      width: 70%;
      height: 100%;
    }
    .sales-view-list {
      flex: 1;
      width: 100%;
      height: 100%;
      overflow: hidden;
      .sales-view-title {
        margin-top: 20px;
        font-size: 12px;
        color: #666;
        font-weight: 500;
      }
      .list-item-wrapper {
        margin-top: 15px;
        .list-item {
          display: flex;
          align-items: center;
          font-size: 12px;
          height: 20px;
          padding: 6px 20px 6px 0;
          .list-item-no {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 20px;
            height: 20px;
            color: #333;
            &.top-no {
              background: #000;
              border-radius: 50%;
              color: #fff;
              font-weight: 500;
            }
          }
          .list-item-name {
            margin-left: 10px;
            color: #333;
          }
          .list-item-money {
            flex: 1;
            text-align: right;
          }
        }
      }
    }
  }
}
</style>